<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="日期">
        <el-date-picker
          v-model="dateRange"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 240px"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['equipment:deviceCheckPlan:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['equipment:deviceCheckPlan:edit']"
          >修改</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:user:remove']"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['equipment:deviceCheckPlan:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="printBox"
          v-hasPermi="['equipment:deviceCheckPlan:print']"
          >打印</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      @selection-change="handleSelectionChange"
      fit
      v-loading="loading"
      border
      :data="dataList"
    >
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column
        show-overflow-tooltip
        label="设备管理员"
        prop="managerUserName"
      />
      <el-table-column
        label="设备名称"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <span>{{ handleString(scope.row.itemList) || "--" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="质量负责人"
        prop="massUserName"
      />
      <el-table-column show-overflow-tooltip label="日期" prop="planDate" />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="240"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            v-hasPermi="['equipment:deviceCheckPlan:info']"
            @click="handleDetail(scope.row)"
            >详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['equipment:deviceCheckPlan:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['equipment:deviceCheckPlan:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 打印模板位置 -->
    <PrintBox
      :pageNum="queryParams.pageNum"
      :total="total"
      :printRow="printList"
      :isVertical="false"
      id="printBox"
      :checkNum="9"
      :checkLabel="'itemList'"
    >
      <template slot="title" slot-scope="scope">
        设备期间核查计划表STET/B/23-1
      </template>
      <template slot-scope="scope">
        <!-- 表格  class  table-page  必须-->
        <table
          style="border-collapse: collapse"
          border="1"
          cellspacing="0"
          class="table-page"
        >
          <thead>
            <tr class="table_header">
              <th style="width: 20mm">序号</th>
              <th style="width: 30mm">计划核查日期</th>
              <th style="width: 45mm">设备名称/型号</th>
              <th style="width: 45mm">设备编号</th>
              <th style="width: 70mm">扩展不确定度/最大允差/准确度等级</th>
              <th style="width: 50mm">期间核查作业指导书</th>
              <th style="width: 45mm">期间核查方式</th>
              <th style="width: 35mm">核查周期(月)</th>
              <th style="width: 35mm">实际核查日期</th>
              <th style="width: 35mm">核查记录表编号</th>
              <th style="width: 35mm">结果</th>
            </tr>
          </thead>

          <!-- 同列表接口数据 -->
          <tbody>
            <tr v-for="(e, i) in 9">
              <td>{{ scope.item.itemList[i] ? i + 1 : "" }}</td>
              <td>
                {{
                  (scope.item.itemList[i] &&
                    scope.item.itemList[i].planCheckDate) ||
                  ""
                }}
              </td>
              <td>
                {{
                  scope.item.itemList[i] && scope.item.itemList[i].deviceName
                }}
              </td>
              <td>
                {{
                  scope.item.itemList[i] && scope.item.itemList[i].deviceCode
                }}
              </td>
              <td>
                {{
                  (scope.item.itemList[i] &&
                    scope.item.itemList[i].devicePrecision) ||
                  ""
                }}
              </td>
              <td>
                {{
                  (scope.item.itemList[i] && scope.item.itemList[i].guide) || ""
                }}
              </td>
              <td>
                {{
                  (scope.item.itemList[i] &&
                    dict.type.sys_device_check_type.filter((item) => {
                      return item.value == scope.item.itemList[i].checkType;
                    })[0].label) ||
                  ""
                }}
              </td>
              <td>
                {{
                  (scope.item.itemList[i] &&
                    scope.item.itemList[i].checkPeriod) ||
                  ""
                }}
              </td>
              <td>
                {{
                  (scope.item.itemList[i] &&
                    scope.item.itemList[i].actCheckDate) ||
                  ""
                }}
              </td>
              <td></td>
              <td></td>
            </tr>
          </tbody>
        </table>
        <!-- 底部信息位置 -->
        <div class="page-bottom" style="page-break-after: always">
          <div style="display: flex; text-align: left">
            <div style="width: 30%">设备管理员:</div>
            <div style="width: 30%">质量负责人:</div>
            <div style="width: 30%">日期:</div>
          </div>
          <div class="desc">注：本表格自2021年6月1日起生效</div>
        </div>
      </template>
    </PrintBox>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listDeviceCheckPlan,
  delDeviceCheckPlan,
} from "@/api/device/deviceCheckPlan.js";
import { deptTreeSelect } from "@/api/system/user.js";
import printStyle from "@/utils/print.js";

export default {
  name: "deviceCheckPlan",
  dicts: ["sys_device_check_type"],

  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      dataList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceName: undefined,
        operUserId: undefined,
        licenseNum: undefined,
        checkDate: null,
      },
      // 日期范围
      dateRange: [],

      userList: [],
      deptOptions: [],
      printList: [],
      infos: [],
    };
  },
  created() {
    const str = sessionStorage.getItem('queryParams');
    if (str) {
      this.queryParams = JSON.parse(str);
      if (this.queryParams.startEndDate) {
        this.dateRange.push(this.queryParams.startEndDate, this.queryParams.endEndDate)
      }
      setTimeout(() => {
        sessionStorage.removeItem('queryParams');
      }, 500)
    }
    this.getList();
    this.getDeptTree();
  },
  methods: {
    printBox() {
      console.log(this.infos);
      //infos  勾选的数据 如果有勾选就打印勾选。
      if (this.infos.length) {
        this.printList = this.infos;
      } else {
        //反之打印当前整个列表
        this.printList = this.dataList;
      }
      setTimeout(() => {
        document.title = "设备期间核查计划表STET/B/23-1";

        this.$print({
          printable: "printBox", //打印区域id
          type: "html", //打印类型是html
          scanStyles: false,
          targetStyles: ["*"],
          style: this.workStyle(),
          onPrintDialogClose: () => {
            document.title = process.env.VUE_APP_TITLE;
          },
        });
      }, 500);
    },
    // 可以自定义样式  printStyle 是公共样式 切勿私自修改
    workStyle() {
      return `${printStyle}  
      .desc{
        text-align: left; font-size: 12px; margin-top: 10px
      }
        table,
      table tr th,
      table tr td {
        border: 1px solid;
        height: 35px;
        padding: 10px;
        
      }
      tr {
        page-break-inside: avoid;
      }
      .table_header{
        font-size:16px;
      }
      `;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.deviceCheckPlanId);
      this.infos = selection;
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    handleString(e) {
      return e.map((i) => i.deviceName).join(",");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      const configId = row.deviceCheckPlanId || this.ids;
      this.$router.push("./addDeviceCheckPlan?isEdit=true&id=" + configId);
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    handleDetail(row) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      this.$router.push(
        "./addDeviceCheckPlan?isInfo=true&id=" + row.deviceCheckPlanId
      );
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const configId = row.deviceCheckPlanId || this.ids;

      this.$modal
        .confirm(
          '是否确认删除设备名称为"' +
            this.handleString(row.itemList) +
            '"的数据项？'
        )
        .then(function () {
          return delDeviceCheckPlan(configId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleAdd() {
      this.$router.push("./addDeviceCheckPlan");
    },
    /** 查询列表 */
    getList() {
      this.loading = true;

      listDeviceCheckPlan(
        this.addDateRangeTime(
          this.queryParams,
          this.dateRange,
          "startEndDate",
          "endEndDate"
        )
      ).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleExport() {
      this.download(
        "system/deviceCheckPlan/export",
        {
          ...this.queryParams,
        },
        `设备期间核查计划表_${this.parseTime(
          new Date().getTime(),
          "{y}-{m}-{d}"
        )}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
  img {
    width: 100%;
  }
}
.table-page {
  width: 100%;
}
</style>
